#include "StreamInput.h"


StreamInput::StreamInput(int outputsCount, bool looping) : Filter(0, outputsCount), looping(looping)
{
	channels = new MatricesList[outputsCount];
	iterators = new MatrixIterator[outputsCount];
	initialized = false;
}


StreamInput::~StreamInput(void)
{
	delete [] channels;
	delete [] iterators;
}

void StreamInput::PushData(const cv::Mat &data, int channel)
{
	channels[channel].push_back(data);
}

void StreamInput::Process()
{
	if (!initialized)
	{
		for (int i = 0; i < GetOutputsCount(); ++i)
		{
			iterators[i] = channels[i].begin();
		}
		initialized = true;
	}

	int outputsCount = GetOutputsCount();
	for (int i = 0; i < outputsCount; ++i)
	{
		if (iterators[i] != channels[i].end())
		{
			outputs[i] = *iterators[i];
			outputValid[i] = true;

			iterators[i]++;
		}
		else
		{
			outputs[i] = cv::Mat();
			outputValid[i] = false;
			endOfStream = true;
		}
	}

	if (looping)
	{
		for (int i = 0; i < outputsCount; ++i)
		{
			if (iterators[i] == channels[i].end())
			{
				iterators[i] = channels[i].begin();
			}
		}
	}
}
